<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="节点名称" prop="nodeName">
        <el-input v-model="queryParams.nodeName" placeholder="请输入节点名称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="实例ID" prop="instanceId">
        <el-input v-model="queryParams.instanceId" placeholder="请输入实例ID" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="节点顺序" prop="nodeNo">
        <el-input v-model="queryParams.nodeNo" placeholder="请输入节点顺序" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="签批类型" prop="approvalTypeOpt">
        <el-input v-model="queryParams.approvalTypeOpt" placeholder="请输入签批类型" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="任务状态" prop="taskStatusOpt">
        <el-select v-model="queryParams.taskStatusOpt" placeholder="请选择任务状态" clearable size="small">
          <el-option v-for="dict in taskStatusOptOptions" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="签收时间" prop="signTime">
        <el-date-picker clearable size="small" style="width: 200px" v-model="queryParams.signTime" type="date"
          value-format="yyyy-MM-dd" placeholder="选择签收时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="办理时间" prop="finishTime">
        <el-date-picker clearable size="small" style="width: 200px" v-model="queryParams.finishTime" type="date"
          value-format="yyyy-MM-dd" placeholder="选择办理时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="办理结果" prop="finishFlagOpt">
        <el-select v-model="queryParams.finishFlagOpt" placeholder="请选择办理结果" clearable size="small">
          <el-option v-for="dict in finishFlagOptOptions" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="当前节点" prop="isCurrentOpt">
        <el-select v-model="queryParams.isCurrentOpt" placeholder="请选择当前节点" clearable size="small">
          <el-option v-for="dict in isCurrentOptOptions" :key="dict.dictValue" :label="dict.dictLabel"
            :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['labdata:task:add']">
          新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['labdata:task:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['labdata:task:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5" v-show="false">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['labdata:task:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="任务ID" align="center" prop="taskId" />
      <el-table-column label="节点名称" align="center" prop="nodeName" />
      <el-table-column label="实例ID" align="center" prop="instanceId" />
      <el-table-column label="节点顺序" align="center" prop="nodeNo" />
      <el-table-column label="签批类型" align="center" prop="approvalTypeOpt" />
      <el-table-column label="任务状态" align="center" prop="taskStatusOpt" :formatter="taskStatusOptFormat" />
      <el-table-column label="签收人员" align="center" prop="signUser" />
      <el-table-column label="签收时间" align="center" prop="signTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="办理时间" align="center" prop="finishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="办理结果" align="center" prop="finishFlagOpt" :formatter="finishFlagOptFormat" />
      <el-table-column label="当前节点" align="center" prop="isCurrentOpt" :formatter="isCurrentOptFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['labdata:task:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['labdata:task:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改流程任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="节点名称" prop="nodeName">
          <el-input v-model="form.nodeName" placeholder="请输入节点名称" />
        </el-form-item>
        <el-form-item label="实例ID" prop="instanceId">
          <el-input v-model="form.instanceId" placeholder="请输入实例ID" />
        </el-form-item>
        <el-form-item label="节点顺序" prop="nodeNo">
          <el-input v-model="form.nodeNo" placeholder="请输入节点顺序" />
        </el-form-item>
        <el-form-item label="签批类型" prop="approvalTypeOpt">
          <el-input v-model="form.approvalTypeOpt" placeholder="请输入签批类型" />
        </el-form-item>
        <el-form-item label="任务状态" prop="taskStatusOpt">
          <el-select v-model="form.taskStatusOpt" placeholder="请选择任务状态">
            <el-option v-for="dict in taskStatusOptOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="办理角色" prop="nodeRoles">
          <el-input v-model="form.nodeRoles" placeholder="请输入办理角色" />
        </el-form-item>
        <el-form-item label="签收人员" prop="signUser">
          <el-input v-model="form.signUser" placeholder="请输入签收人员" />
        </el-form-item>
        <el-form-item label="签收时间" prop="signTime">
          <el-date-picker clearable size="small" style="width: 200px" v-model="form.signTime" type="date"
            value-format="yyyy-MM-dd" placeholder="选择签收时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="办理时间" prop="finishTime">
          <el-date-picker clearable size="small" style="width: 200px" v-model="form.finishTime" type="date"
            value-format="yyyy-MM-dd" placeholder="选择办理时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="办理意见" prop="finishComments">
          <el-input v-model="form.finishComments" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="办理结果" prop="finishFlagOpt">
          <el-select v-model="form.finishFlagOpt" placeholder="请选择办理结果">
            <el-option v-for="dict in finishFlagOptOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="当前节点" prop="isCurrentOpt">
          <el-select v-model="form.isCurrentOpt" placeholder="请选择当前节点">
            <el-option v-for="dict in isCurrentOptOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listTask,
    getTask,
    delTask,
    addTask,
    updateTask,
    exportTask
  } from "@/api/labdata/flowTask";

  export default {
    name: "Task",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 流程任务表格数据
        taskList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 任务状态字典
        taskStatusOptOptions: [],
        // 办理结果字典
        finishFlagOptOptions: [],
        // 当前节点字典
        isCurrentOptOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          nodeName: null,
          instanceId: null,
          nodeNo: null,
          approvalTypeOpt: null,
          taskStatusOpt: null,
          signTime: null,
          finishTime: null,
          finishFlagOpt: null,
          isCurrentOpt: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          nodeName: [{
            required: true,
            message: "节点名称不能为空",
            trigger: "blur"
          }],
          instanceId: [{
            required: true,
            message: "实例ID不能为空",
            trigger: "blur"
          }],
          approvalTypeOpt: [{
            required: true,
            message: "签批类型不能为空",
            trigger: "blur"
          }],
          taskStatusOpt: [{
            required: true,
            message: "任务状态不能为空",
            trigger: "change"
          }],
          nodeRoles: [{
            required: true,
            message: "办理角色不能为空",
            trigger: "blur"
          }],
          isCurrentOpt: [{
            required: true,
            message: "当前节点不能为空",
            trigger: "change"
          }]
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("taskStatusOpt").then(response => {
        this.taskStatusOptOptions = response.data;
      });
      this.getDicts("finishFlagOpt").then(response => {
        this.finishFlagOptOptions = response.data;
      });
      this.getDicts("isOrNotOpt").then(response => {
        this.isCurrentOptOptions = response.data;
      });
    },
    methods: {
      /** 查询流程任务列表 */
      getList() {
        this.loading = true;
        listTask(this.queryParams).then(response => {
          this.taskList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 任务状态字典翻译
      taskStatusOptFormat(row, column) {
        return this.selectDictLabel(this.taskStatusOptOptions, row.taskStatusOpt);
      },
      // 办理结果字典翻译
      finishFlagOptFormat(row, column) {
        return this.selectDictLabel(this.finishFlagOptOptions, row.finishFlagOpt);
      },
      // 当前节点字典翻译
      isCurrentOptFormat(row, column) {
        return this.selectDictLabel(this.isCurrentOptOptions, row.isCurrentOpt);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          taskId: null,
          nodeName: null,
          instanceId: null,
          nodeNo: null,
          approvalTypeOpt: null,
          taskStatusOpt: null,
          nodeRoles: null,
          signUser: null,
          signTime: null,
          finishTime: null,
          finishComments: null,
          finishFlagOpt: null,
          isCurrentOpt: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.taskId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加流程任务";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const taskId = row.taskId || this.ids
        getTask(taskId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改流程任务";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.taskId != null) {
              updateTask(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addTask(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const taskIds = row.taskId || this.ids;
        this.$confirm('是否确认删除流程任务编号为"' + taskIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTask(taskIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有流程任务数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTask(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }
  };
</script>
